//
//  HMToplistDetailListVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-05.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMToplistDetailListVC.h"
#import "HMToplistDetailCell.h"
#import "HMToplist.h"
#import "HMToplistDetailModel.h"
#import "UIImage+ImageEffects.h"
#import "HMRecommendDetailVC.h"
#import "HMHomeToplistModel.h"
//#import "HMPayViewController.h"
#import "UIViewController+Extension.h"

@interface HMToplistDetailListVC ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UILabel *sumPriceLabel;
@property(strong,nonatomic)UITableView *detailListTableView;
@property(strong,nonatomic)UIView *buyView;
@property(strong,nonatomic)UIImageView *backImageView;

@property (nonatomic, assign) CGFloat allPrice;
@property (nonatomic, strong) NSMutableArray *btnArray;

@property(strong,nonatomic)NSArray<HMToplist *> *toplistDetailArray;



@end
static NSString *tagOfDetailListTableViewCell = @"detailListTableViewCell";
@implementation HMToplistDetailListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //    创建屏幕最下方购买的View
    [self createBuyView];
    
    //    创建列表的tableView
    [self createDetailListView];
    
    //    请求网络数据
    [HMToplistDetailModel requestToplistDetailBySeriesid:self.series_id.integerValue andFinished:^(id responseObject) {
        self.toplistDetailArray = responseObject;
        
        self.titleLabel.text = self.toplistModel.series_name;
        self.contentLabel.text = self.toplistModel.series_content;
        [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.toplistModel.series_image_url] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
        
        self.backImageView.image =  [self.coverImageView.image applyDarkEffect];
         
//        UIImage *blurImage = [[UIImage imageNamed:@"placeHolder"] applyLightEffect];
//        UIImageView *backImageView = [[UIImageView alloc]initWithImage:blurImage];
        
        [self.detailListTableView reloadData];

    } Failed:^(NSError *error) {
        HMLog(@"%@",error);
    }];
    
//    设置内边距，让detailListTableView往下偏移导航栏+电池栏的高度
    self.detailListTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    
    //-------------设置导航栏----------------------    
    [super setupNavRightItemsByTwo];
    
    
    //-------------设置导航栏结束----------------------
}

//    创建列表的tableView
- (void)createDetailListView{
    UITableView *detailListTableView = [[UITableView alloc]init];
    self.detailListTableView = detailListTableView;
    [self.view addSubview:detailListTableView];
    
    [detailListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.equalTo(self.buyView.mas_top).offset(0);
    }];
    
    detailListTableView.delegate = self;
    detailListTableView.dataSource = self;
    
    [detailListTableView registerClass:[HMToplistDetailCell class] forCellReuseIdentifier:tagOfDetailListTableViewCell];
    
    detailListTableView.estimatedRowHeight = 145;
    detailListTableView.rowHeight = UITableViewAutomaticDimension;
    
    //    创建头部制图
    detailListTableView.tableHeaderView = [self createHeaderView];
}

//创建头部视图
- (UIView *)createHeaderView{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor lightGrayColor];
    
//    对头部的图片进行高斯模糊处理
    UIImage *blurImage = [[UIImage imageNamed:@"placeHolder"] applyLightEffect];
    UIImageView *backImageView = [[UIImageView alloc]initWithImage:blurImage];
    backImageView.contentMode = UIViewContentModeScaleToFill;
    [headerView addSubview:backImageView];
    self.backImageView = backImageView;
    
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    //    创建系列标题
    self.titleLabel = [UILabel hm_labelWithText:@"得到专栏作者作品合集" fontSize:k_FONT_BIG color:[UIColor whiteColor]];
    [backImageView addSubview:self.titleLabel];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(7);
        make.left.offset(20);
    }];
    
    //    系列图片
    
    self.coverImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"201612271441573272"]];
    [backImageView addSubview:self.coverImageView];
    
    self.coverImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(7);
        make.left.equalTo(self.titleLabel.mas_left).offset(0);
        make.width.offset(85);
        make.height.mas_lessThanOrEqualTo(74);
    }];
    
    
    //    系列简介
    self.contentLabel = [UILabel hm_labelWithText:@"每晚九点的创业陪伴,一步一步成为创业达人" fontSize:k_FONT_NORMAL color:[UIColor whiteColor]];
    [backImageView addSubview:self.contentLabel];
    
    self.contentLabel.numberOfLines = 2;
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(29);
        make.left.equalTo(self.coverImageView.mas_right).offset(16);
        make.right.offset(-36);
    }];
    
    
    
    
    [backImageView layoutIfNeeded];
    backImageView.frame = CGRectMake(0, 0, k_SCREEN_WIDTH, CGRectGetMaxY(self.coverImageView.frame));
    headerView.frame = backImageView.frame;
    return headerView;
}
//创建屏幕最下方的购买view
- (void)createBuyView{
    UIView *buyView = [[UIView alloc]init];
    self.buyView = buyView;
    [self.view addSubview:buyView];
    
    buyView.backgroundColor = [UIColor colorWithHex:0x333333];
    
    [buyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(50);
    }];
    
    //    创建购买按钮
    UIButton *buyBtn = [[UIButton alloc]init];
    [buyView addSubview:buyBtn];
    
    [buyBtn hm_setTitle:@"一键购买" andTiTleColorWithHex:0xffffff forState:UIControlStateNormal];
    buyBtn.backgroundColor = [UIColor colorWithHex:0xffa42f];
    //    修改button的圆角
    buyBtn.layer.cornerRadius = 5.0;
    
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.bottom.offset(-5);
        make.right.offset(-10);
        make.width.offset(100);
    }];
    
    [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //    购买的提示文字
    UILabel *capionLabel = [UILabel hm_labelWithText:@"已经扣除已购买部分" fontSize:k_FONT_SMALL colorWithHex:0x656565];
    [buyView addSubview:capionLabel];
    
    [capionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.bottom.offset(-7);
    }];
    
    
    UILabel *sumCapionLabel = [UILabel hm_labelWithText:@"合计：" fontSize:k_FONT_BIG colorWithHex:0xffffff];
    //    字体加粗
    sumCapionLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:k_FONT_BIG];
    
    [buyView addSubview:sumCapionLabel];
    
    [sumCapionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(7);
        make.left.equalTo(capionLabel.mas_left).offset(0);
    }];
    
    //    合计的价格标签
    self.sumPriceLabel = [UILabel hm_labelWithText:@"¥0.00" fontSize:k_FONT_BIG colorWithHex:0xffa42f];
    //    字体加粗
    self.sumPriceLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:k_FONT_BIG];
    [buyView addSubview:self.sumPriceLabel];
    
    [self.sumPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sumCapionLabel.mas_centerY).offset(0);
        make.left.equalTo(sumCapionLabel.mas_right).offset(0);
    }];
}

#pragma 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.toplistDetailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HMToplistDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:tagOfDetailListTableViewCell forIndexPath:indexPath];
    
    cell.toplistDetailModel = self.toplistDetailArray[indexPath.row];
    [cell.shoppingCartBtn addTarget:self action:@selector(shoppingCartBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.shoppingCartBtn.tag = indexPath.row;
    [self.btnArray addObject:cell.shoppingCartBtn];
    
    return cell;
}

- (void)shoppingCartBtnClick:(UIButton *)btn
{
    if (btn.selected)
    {
        self.allPrice += [self.toplistDetailArray[btn.tag] price].floatValue;
        
    }
    else
    {
        self.allPrice -= [self.toplistDetailArray[btn.tag] price].floatValue;
    }
    
    self.sumPriceLabel.text = [NSString stringWithFormat:@"¥%.2f", self.allPrice];
}

- (void)buyBtnClick:(UIButton *)btn
{
    [self showHudAuto:@"余额不足,请充值"];

}
//选中详细，进行跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HMRecommendDetailVC *detailVC = [[HMRecommendDetailVC alloc]init];
    detailVC.resource_id = self.toplistDetailArray[indexPath.row].resource_id;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
